package Processes;

import threadedSimulator.Cpu;

/**
 * a producer class that does math on a number and when its done sends it to a consumer
 * @author Harris
 */
public class ProducerProcess extends Processes{
    
    /**
     * the consumer that is linked to the producer.
     */
    ConsumerProcess my_consumer;
    
    /**
     * constructs the producer. 
     * @param the_cpu the cpu
     * @param the_numberof_pro_con the number of producers and consumers
     * @param the_consumer the consumer linked to producer.
     */
    public ProducerProcess(Cpu the_cpu, String the_numberof_pro_con, ConsumerProcess the_consumer) {
	super(the_cpu, the_numberof_pro_con, "Producer");
	my_priority = 2;
        my_consumer = the_consumer;
    }

    /**
     * runs the process and adds the square root of itself to itself.
     */
    @Override
    public void run() {
        double temp = 0;
        running = true;
        while (running && my_current_index <= MAX_VALUE) {
            for (int i = 0; i < 10; i++) {
                temp += Math.sqrt(temp);
            }
            
            
            
            my_current_index++;
            if (my_current_index == MAX_VALUE) { //ends the producers work.
            	has_finished = true;
                my_consumer.producer_is_finished = has_finished;
                running = false;
            }	
        }
        
        my_consumer.my_producer_value = temp;//sends the value to consumer.
    }
}
